package com.hzlj.position.locate.controller;


import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.position.config.common.dto.common.ReportLocateDTO;
import com.hzlj.position.locate.common.dto.reportCommon.ReportHandleDTO;
import com.hzlj.position.locate.common.dto.reportCommon.ReportNoticeResultDTO;
import com.hzlj.position.locate.common.dto.reportInspection.ReportInspectionArtificialNoticeDTO;
import com.hzlj.position.locate.common.dto.reportInspection.ReportInspectionCorrectSendNoticeDTO;
import com.hzlj.position.locate.common.dto.reportInspection.ReportInspectionQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInspection.ReportInspectionWithJzJbxxDTO;
import com.hzlj.position.locate.service.ReportInspectionService;
import com.hzlj.position.locate.service.ReportInspectionSyncService;
import com.hzlj.sqjz.config.common.dto.common.handle.HandleCommonQueryResultDTO;
import com.hzlj.sqjz.system.service.JgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 【通信联络核查-核查】接口
 *
 * @author lifh
 * @date 2023-07-21 14:20:18
 */
@Slf4j
@RestController
@RequestMapping("/position/report/inspection")
public class ReportInspectionController {
    @Resource
    private JgService jgService;
    @Resource
    private ReportInspectionService reportInspectionService;
    @Resource
    private ReportInspectionSyncService reportInspectionSyncService;

    /**
     * 处理异常
     */
    @PostMapping("/handle")
    public ResultDTO<Boolean> handle(@RequestBody ReportHandleDTO dto) {
        //更新分表
        this.reportInspectionService.handle(dto);
        return ResultDTO.ok();
    }


    /**
     * 获取处理状态数据
     */
    @GetMapping(value = "/handleResult/{id}")
    public ResultDTO<HandleCommonQueryResultDTO> getHandleResult(@PathVariable("id") String id, Date belongDay) {
        return new ResultDTO<>(
                reportInspectionService.getHandleResult(id, belongDay)
        );
    }


    /**
     * 上报位置(人工核验/自动核验)
     * reportType：1:自动核验  2:手工核验
     */
    @PutMapping(value = "/{reportType}")
    public ResultDTO<Boolean> report(@PathVariable("reportType") String reportType,
                                     @RequestBody ReportLocateDTO dto) {
        log.info("核验位置上报：reportType={},dto={}", reportType, JsonUtils.obj2json(dto));
        dto.setReportType(reportType);
        reportInspectionService.reportLocate(dto);
        return ResultDTO.ok();
    }

    /**
     * 发起核验-人工核验
     */
    @PutMapping(value = "/artificial")
    public ResultDTO<ReportNoticeResultDTO> reportInspectionArtificial(@RequestBody ReportInspectionArtificialNoticeDTO dto) {
        log.info("发起人工核验：dto={}", JsonUtils.obj2json(dto));
        return new ResultDTO<>(reportInspectionSyncService.syncReportInspectionArtificial(dto));
    }

    /**
     * 手动作业-自动核查
     */
    @GetMapping(value = "/auto/job")
    public ResultDTO<Integer> reportInspectionAutoJob() {
        return new ResultDTO<>(reportInspectionSyncService.syncReportInspectionAuto());
    }


    /**
     * 发起核验-纠偏
     */
    @PutMapping("/correct")
    public ResultDTO<MessageNoticeResultDTO> reportInspectionCorrect(@RequestBody ReportInspectionCorrectSendNoticeDTO dto) {
        log.info("【新增信息】定位记录-发起纠偏通知={}", dto);
        return new ResultDTO<>(
                reportInspectionSyncService.syncReportInspectionCorrect(dto)
        );
    }


    /**
     * 导出 通信联络核查
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       ReportInspectionQueryParamDTO dto) {
        List<ReportInspectionWithJzJbxxDTO> list = reportInspectionService.listReportInspection(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "核查记录",
                "核查记录",
                jgService.export3Level(ReportInspectionWithJzJbxxDTO::getJgCode, ReportInspectionWithJzJbxxDTO::getJgMc)
                        .add(ReportInspectionWithJzJbxxDTO::getXm, "姓名")
                        .add(ReportInspectionWithJzJbxxDTO::getXbText, "性别")
                        .add(ReportInspectionWithJzJbxxDTO::getSjh, "手机号")
                        .add(ReportInspectionWithJzJbxxDTO::getReportTypeText, "核查类型")
                        .add(ReportInspectionWithJzJbxxDTO::getNoticeText, "核查状态")
                        .add(ReportInspectionWithJzJbxxDTO::getActualSpotCheckTime, "核查时间")
                        .add(ReportInspectionWithJzJbxxDTO::getReportStatusText, "上报状态")
                        .add(ReportInspectionWithJzJbxxDTO::getReportAddress, "位置信息")
                        .add(ReportInspectionWithJzJbxxDTO::getReportTime, "上报时间")
                        .add(ReportInspectionWithJzJbxxDTO::getCrossBorderText, "位置状态")
                        .add(ReportInspectionWithJzJbxxDTO::getHandleStatusText, "处理状态")
                        .getFields(),
                list
        );
    }


    /**
     * 列表查询 通信联络核查-核查
     */
    @GetMapping("/list")
    public ResultDTO<List<ReportInspectionWithJzJbxxDTO>> listReportInspection(ReportInspectionQueryParamDTO dto) {
        log.debug("【分页列表】信息通信联络核查-核查,ReportInspectionQueryParamDTO={}", dto);
        return new ResultDTO<>(reportInspectionService.listReportInspection(dto));
    }

    /**
     * 分页查询 通信联络核查-核查
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<ReportInspectionWithJzJbxxDTO>> pageReportInspection(ReportInspectionQueryParamDTO dto, PageParamDTO pageParamDTO) {
        log.debug("【查询分页列表】信息通信联络核查-核查,ReportInspectionQueryParamDTO={},pageParamDTO={}", dto, pageParamDTO);
        return new ResultDTO<>(reportInspectionService.pageReportInspection(dto, pageParamDTO));
    }

}

